#从sklearn导入数据集
import numpy as np
from sklearn import datasets
irises=datasets.load_iris()
X_train=irises.data
y_train=irises.target

#导入模块，进行数据训练集和测试数据集的分割
from sklearn import model_selection
#train_test_split返回一个列表，有四个值，分别接受
X_train,X_test,y_train,y_test=model_selection.train_test_split(X_train,y_train,test_size=0.5)

#数据单位归一化
# from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import StandardScaler
#生成归一化对象
std_scaler=StandardScaler()
#归一化计算，获取均值std.mean_和方差
std_scaler.fit(X_train)
#进行归一化操作
X_train_std=std_scaler.transform(X_train)
X_test_std=std_scaler.transform(X_test)

#knn计算
from sklearn.neighbors import KNeighborsClassifier
#生成knn算法器
myknn=KNeighborsClassifier()
#训练模型
myknn.fit(X_train_std,y_train)
#得分测试
myscore=myknn.score(X_test_std,y_test)
print(myscore)


# print("测试数据集预测结果：",myknn.predict(X_test_std))
# print("测试数据集真实标签：",y_test)


#knn算法预测分类
# X_predict=np.array([1.31,3.9,1.3,1.9,3.31,6.9,3.3,1.9])
# print(X_predict)
# X_predict=X_predict.reshape(-1,4)
# print(X_predict)
# X_predict_std=std_scaler.transform(X_predict)
# print(myknn.predict(X_predict_std))